import { defineStore } from "pinia";

import type { DatasetRecord, DatasetState } from "@/types/data/dataset";

export const useDatasetStore = defineStore("datasetStore", {
  state: (): DatasetState => ({
    datasets: [],
    total: 0,
    currentPage: 1,
    isLoaded: false,
  }),
  actions: {
    setDatasets(datasets: DatasetRecord[], total: number, currentPage: number) {
      const mappedDatasets = datasets.map(record => ({
        datasetId: record.datasetId,
        title: record.name,
        description: record.description,
        date: record.createdTime,
        views: record.clickNum ? record.clickNum.toString() : '0',
        likes: record.favoriteNum ? record.favoriteNum.toString() : '0',
        comments: 0, // 如果有评论数据，请替换此处
        downloads: record.downloadNum ? record.downloadNum.toString() : '0',
        shares: record.shareNum ? record.shareNum.toString() : '0',
        username: record.creatorName,
        favorites: record.favoriteNum ? record.favoriteNum.toString() : '0',
        tags: record.tags || [],
        image: record.imagePath,
        link: record.url,
      }));

      this.datasets = mappedDatasets;
      this.total = total;
      this.currentPage = currentPage;
      this.isLoaded = true; // 设置为已加载
    },
  },
});
